<template>
  <div class="wuliu">
    <top></top>
    <div v-loading='loading' style="padding: 20px;">

      <div class="kflexc">
        <el-upload ref="upload" action="#" :before-upload="uploasd2" :show-file-list="false" accept=".xlsx,.xls">
           <el-button type="danger" round style="margin-right: 20px;">导入自有渠道订单</el-button>
        </el-upload>
        <div @click="xiazai" style="margin-right: 20px; font-size: 12px; color: #999; cursor: pointer;">下载模板</div>

        <el-input v-if="shopid == 50481 || shopid == 304676" v-model="all.log_name" style="width: 250px; margin-right: 20px;" placeholder="表名称"></el-input>
        <el-button v-if="shopid == 50481 || shopid == 304676" round @click="search()" type="danger">搜索</el-button>

      </div>
      <el-table :data="list" border style="width: 100%; margin: 20px 0;" ref="multipleTable" >

        <el-table-column align='center' prop='log_name' label="表名"></el-table-column>
        <el-table-column align='center' prop='created_at' label="导入时间"></el-table-column>
        <el-table-column align='center' prop='orders_count' label="导入订单数"></el-table-column>
        <el-table-column align='center' label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="mini" v-if="scope.row.origin_file" @click="xiazai2(scope.row.origin_file)">下载导入表</el-button>
            <el-button type="text" size="mini" v-if="scope.row.result_file" @click="xiazai3(scope.row.result_file)">下载导入结果</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination style="text-align: right;" background layout="total,prev, pager, next" :current-page="all.page" @current-change="fanye" :page-size="all.page_size" :total="total"></el-pagination>
    </div>

  </div>
</template>

<script>
  import top from '../top.vue'
  import axios from 'axios'
  import moment from "moment";
  import XLSX from 'xlsx';
  import {
      read,utils
    } from 'xlsx'

  export default {
    components:{
      top
    },
    name: "wuliu",
    data() {
      return {
        loading:false,
        list:"",
        all:{
          page:1,
          page_size:20,
          log_name:""
        },
        total:0,
        shopid:''
      }
    },
    created() {
      //店铺权限问题
			this.shopid = JSON.parse(localStorage.getItem('SHOP')).shop_id;
      this.getlist()
      //获取当前年月
      // this.getNowDate()
      // console.log(this.vsdate())


    },
    mounted() {

    },
    methods: {

      readExcel1(files) {
        return new Promise((res, rej) => {
          var that = this;
          if (files.length <= 0) {
            return;
          }
          if (!/\.(xls|xlsx)$/.test(files[0].name.toLowerCase())) {
            this.$Message.error('上传格式不正确，请上传xls或者xlsx格式');
            return;
          }
          const fileReader = new FileReader();
          fileReader.onload = (ev) => {
            try {
              const data = ev.target.result;
              const workbook = read(data, {
                type: 'binary'
              });
              // 取第一张表
              const wsname = workbook.SheetNames[0];
              // 生成json表格内容
              const ws = utils.sheet_to_json(workbook.Sheets[wsname]);
              console.log(ws);
              // 后续为自己对ws数据的处理
              res(ws)
            } catch (e) {
              return false;
              rej(e)
            }
          };
          fileReader.readAsBinaryString(files[0]);
        })

      },

      uploasd2(file, fileList) {
        let obj = new FormData()
        obj.append('file',file)
        axios.post('/api/plan-market/tool/own/order/import',obj)
          .then(response => {
            if (response.data.msg.code == 0) {
              this.$message.success(`导入成功`);
              this.getlist()
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
        // var that = this
        // let files = {
        //   0: file
        // }
        // let data = []

        // let ff = this.readExcel1(files);
        // ff.then((res) => {

        //   res.forEach(item => {
        //     let obj = {
        //       mobile: item['业务类型'],
        //       mobile: item['用户姓名'],
        //       remark: item['配送联系电话'],
        //       remark: item['配送省市区县'],
        //       remark: item['用户下单时间'],
        //       remark: item['物流拒收原因'],
        //     }
        //     data.push(obj)
        //   })

        //   axios.post('/api/tool/import/ljquchong',{ids:data})
        //     .then(response => {
        //       if (response.data.msg.code == 0) {
        //         this.$message.success(`导入成功`);
        //         this.getlist()
        //       } else {
        //         this.$message.error(response.data.msg.info);
        //       }
        //     })
        // })
      },

      //统计

      search: function() {
        this.all.page = 1
        this.getlist()
      },
      //翻页
      fanye: function(page) {
        this.loading = true
        this.all.page = page
        this.getlist()
      },
      //获取列表
      getlist: function() {
        axios.get('/api/plan-market/tool/own/order/list',{params:this.all})
          .then(response => {
            if (response.data.msg.code == 0) {
              this.list = response.data.data.data
              this.total = response.data.data.total
              this.loading = false
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
      },

      //下载模板
      xiazai: function() {
        const a = document.createElement('a');
        a.setAttribute('download', '订单重复查询.xlsx');
        a.setAttribute('href',location.origin + location.pathname + 'static/ziyoudingdan.xlsx');
        a.click();
      },

      //真下载
      xiazai2: function(dizhi) {
        var a = document.createElement('a');
        a.setAttribute('href',location.origin + '/' + dizhi);
        a.click();
      },
      xiazai3: function(dizhi) {
        var a = document.createElement('a');
        a.setAttribute('href',dizhi);
        a.click();
      },

    }
  }
</script>

<style lang="scss" scoped>
  .wuliu {
    .powertop {
    	display: flex;
    	background:#f9f9f9;
    }
    .powertopli {
    	flex-basis: 180px;
    	line-height: 50px;
    	border-bottom: 3px #F9F9F9 solid;
    	color: #727272;
    	border-right: 1px #F9F9F9 solid;
    	font-size: 16px;
    	text-align: center;
    }
    .router-link-active {
    	background: #FFFFFF;
    	border-bottom: 3px #2974FF solid;
    }
    /deep/ .el-pagination.is-background .btn-prev:disabled{ background: rgba(246, 246, 246, 0.3); border: 1px rgba(167, 167, 167, 0.3) solid;}
    /deep/ .el-pagination.is-background .btn-next:disabled{ background: rgba(246, 246, 246, 0.3); border: 1px rgba(167, 167, 167, 0.3) solid;}
    /deep/ .el-pagination.is-background .btn-next{ background: rgba(246, 246, 246,1); border: 1px rgba(167, 167, 167,1) solid;}
    /deep/ .el-pagination.is-background .btn-prev{ background: rgba(246, 246, 246,1); border: 1px rgba(167, 167, 167,1) solid;}
    /deep/ .el-pagination.is-background .el-pager li:not(.disabled).active{ background-color: #2974FF;}
    /deep/ .el-pagination.is-background .el-pager li:not(.disabled).active:hover {
        color: #fff;
    }
    /deep/ .el-pagination.is-background .el-pager li:not(.disabled):hover {
        color: #2974FF;
    }
    /deep/ .el-pager li{ border-radius: 14px; background: rgba(255, 255, 255, 1); border: 1px rgba(167, 167, 167, 0.3) solid;}
    /deep/ .btn-prev{ border-radius: 14px; padding: 0 10px;}
    /deep/ .btn-next{ border-radius: 14px; padding: 0 10px;}

    /deep/ .el-table th{ background: rgb(246, 246, 246);}
    /deep/ .el-input__inner{ line-height: 36px; height: 36px; font-size: 12px; background: rgb(247, 248, 250);  border: 0;}
    /deep/ .el-date-editor .el-range-input{ background: rgb(247, 248, 250); }
    /deep/ .el-button{ padding: 11px 23px !important;}
    /deep/ .el-button--mini{ padding: 6px 15px !important;}
    /deep/ .el-button--danger {
        color: #FFF;
        background-color: #2974FF;
        border-color: #2974FF;
    }
    .team {
    	background: #F9F9F9;
    }
  }
</style>
